var updateWindow;
var loadDiv = null;
$(function () {
	loadDiv = $("#loadDiv").loading({container:$("#updatePasswordDialog")});//初始化提示操作执行中	 
	//初始化
	updatePassword();	
	//初始化更新窗口
    updateWindow = $("#updatePasswordDialog");
    updateWindow.dialog({
        autoOpen: false,
        width: 400,
        height: 220,
        resizable: false,
        title: "修改用户密码",
        modal: true,
        buttons: {
            "保存": function () {
                $("#updatePasswordForm").submit();
            },
            "取消": function () {
                $(this).dialog("close");
            }
        },
        close: function(){
        }
    });
    updateWindow.dialog("open");
});



function updatePassword(){
//添加jquery自定义验证，通过正则表达式验证密码
jQuery.validator.addMethod("passwordValidate",function(value,element){
	
	var regex = /^[0-9a-zA-Z_]{6,12}$/;
	return this.optional(element)||(regex.test(value));
		
},"密码6-12位，由字母、数字或下划线组成，请重新输入！");
//声明消息提示
var toolTips = new Object();
//设置表单验证
var validator = $("#updatePasswordForm").validate({    	
	rules:{
		//jQuery远程后台验证原密码是否正确
		oldPassword:{
    		required:true,
    		remote:{
    			url: getPath() +  "validateOldPassword",
    			type: "post",
    			data: {
    				password:function(){
    					return $("#oldPassword").val();
    				}
        		}
        	}
		},
    	password:{
    		required:true,
    		passwordValidate:true
    	},
    	password2:{
    		required:true,
    		passwordValidate:true,
    		equalTo:"#newPassword"
    		
    	}
    },
    messages:{
    	oldPassword:{
    		required:"请输入原密码！",
    		remote:"原密码不正确！"
    		},
		password:{
			required:"请输入新密码！"
		},
    	password2:{
    		required:"请输入确认密码！",
    		equalTo:"确认密码不一致！"
    		}
    },
    errorPlacement: function(eror,element){
        var toolTip = createTooltip(toolTips,element.attr("id"));
        toolTip.setContent(eror.text());
        element.addClass("error-border");

    },
    success:function(label){
        var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
        var toolTip = toolTips[elementId];
        $("#" + elementId).removeClass("error-border");
        toolTip.close();
        toolTip.detach();
    },	    	    	
    submitHandler:function(form){
    	loadDiv.showLoading();
        $.post(getPath() + "updatePassword",
        	{password:$("#newPassword").val()},
            function(data){
        		loadDiv.hideLoading();
                //更新成功，弹出添加成功提示框，当用户点击确定后刷新页面
                createWindow("更新成功").dialog("open");
                updateWindow.dialog("close");
            }
        );
    }
 });
}
